package com.team8.search.controller;

import com.team8.search.pojo.FlightInfoES;
import com.team8.search.repository.FlightInfoRepository;
import com.team8.search.service.FlightInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SearchController {

    @Autowired
    private FlightInfoService flightInfoService;

    @Autowired
    private FlightInfoRepository flightInfoRepository;

    @GetMapping("/search_byport")
    public ResponseEntity<List<FlightInfoES>> searchByport(
            @RequestParam("start") String start,
            @RequestParam("end") String end,
            @RequestParam("date") String date){
        Page<FlightInfoES> flightInfoES;
        try {
            flightInfoES = flightInfoService.queryFlightByPort(start, end, date);
        } catch (ParseException e) {
            return ResponseEntity.ok(null);
        }
        List<FlightInfoES> l = new ArrayList<>();
        return ResponseEntity.ok(flightInfoES.getContent());

    }

    @GetMapping("/search_byffid")
    public ResponseEntity<List<FlightInfoES>> searchByffid(
            @RequestParam("ffid") String ffid,
            @RequestParam("date") String date){
        Page<FlightInfoES> flightInfoES;
        try {
            flightInfoES = flightInfoService.queryFlightByFfid(ffid, date);
        } catch (Exception e) {
            return ResponseEntity.ok(null);
        }
        List<FlightInfoES> l = new ArrayList<>();
        return ResponseEntity.ok(flightInfoES.getContent());

    }

}
